package edu.xhu.behavior.controller;

import edu.xhu.behavior.service.BiliUserChatService;
import edu.xhu.model.behavior.dto.ChatLoginDto;
import edu.xhu.model.behavior.dto.PrivateMessageDto;
import edu.xhu.model.common.dtos.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.bouncycastle.asn1.LocaleUtil;
import org.springframework.web.bind.annotation.*;

@Api(value = "bilibili聊天", tags = "bilibili聊天")
@CrossOrigin
@RestController
@RequiredArgsConstructor
@RequestMapping("/behavior")
public class ChatController {

    private final BiliUserChatService biliUserChatService;

    @GetMapping("/chat/list")
    @ApiOperation("查询当前用户聊天列表")
    public ResponseResult<?> getChatList(@RequestParam("offset") Integer offset) {
        return biliUserChatService.getChatList(offset);
    }

    @GetMapping("/chat/history")
    @ApiOperation("分页查询历史消息")
    public ResponseResult<?> getHistoryDetail(@RequestParam("toId") Long toId,
                                              @RequestParam("detailId") Long detailId) {
        return biliUserChatService.getHistoryDetail(toId, detailId);
    }

    @GetMapping("/chat/create")
    @ApiOperation("创建一个个人聊天")
    public ResponseResult<?> createChat(@RequestParam("toId") Long toId) {
        return biliUserChatService.createChat(toId);
    }

    @PostMapping("/chat/send")
    @ApiOperation("发送个人私聊消息(高并发接口)")
    public ResponseResult<?> sendMessage(@RequestBody PrivateMessageDto dto) {
        return biliUserChatService.sendMessage(dto);
    }

    @GetMapping("/chat/pullOffline")
    @ApiOperation("拉取离线信息")
    public ResponseResult<?> pullOfflineMessage(@RequestParam("toId") Long toId, @RequestParam("minId") Long minId) {
        return biliUserChatService.pullOfflineMessage(toId, minId);
    }

    @PutMapping("/chat/readed")
    @ApiOperation(value = "消息已读", notes = "将会话中接收的消息状态置为已读")
    public ResponseResult<?> readedMessage(@RequestParam("toId") Long toId) {
        return biliUserChatService.readedMessage(toId);
    }

    /**
     * 切换窗口时 更新在线状态以及清除未读
     *
     * @param toId 对方UID
     */
    @GetMapping("/chat/online")
    @ApiOperation(value = "设置在线", notes = "切换窗口时设置用户在线")
    public ResponseResult<?> updateWhisperOnline(@RequestParam("toId") Long toId) {
        return biliUserChatService.updateWhisperOnline(toId);
    }

    /**
     * 登录
     */
    @ApiOperation(value = "聊天登录", notes = "聊天登录")
    @PostMapping("/chat/login")
    public ResponseResult<?> login(@RequestBody ChatLoginDto chatLoginDto) {
        return biliUserChatService.login(chatLoginDto);
    }
}
